package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.dto.userEmp.PositionParamsDto;
import com.zlm.common.dto.userEmp.PositionResponseDto;
import com.zlm.common.enums.*;
import com.zlm.user.emp.dao.EmpPositionSignDao;
import com.zlm.user.emp.dao.EmpPostCollectDao;
import com.zlm.user.emp.dao.OrgPositionDao;
import com.zlm.user.emp.entity.OrgPositionDO;
import com.zlm.user.emp.service.OrgPositionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/4/24 15:34
 */
@Service
public class OrgPositionServiceImpl extends AbstractBaseService<OrgPositionDao, OrgPositionDO> implements OrgPositionService {

    @Autowired
    @Override
    public void setBaseDao(OrgPositionDao orgPositionDao) {
        this.baseDao = orgPositionDao;
    }
    @Autowired
    private EmpPositionSignDao empPositionSignDao;
    @Autowired
    private EmpPostCollectDao empPostCollectDao;

    @Override
    public PageEntity getPositionList(PositionParamsDto positionParamsDto) {
        /*if (null != positionParamsDto.getIsHot() && positionParamsDto.getIsHot().intValue() == 1) {
            positionParamsDto.setSort(3);
        }*/
        int count = baseDao.countPosition(positionParamsDto);
        int pageNumber,pageSize;
        if (null == positionParamsDto.getPageSize()|| positionParamsDto.getPageSize()>= CommonConstants.maxPageSize) {
            pageSize = CommonConstants.pageSize;
        }else {
            pageSize = positionParamsDto.getPageSize();
        }

        if (null == positionParamsDto.getPageNumber()) {
            pageNumber = CommonConstants.pageNumber;
        }else {
            pageNumber = (positionParamsDto.getPageNumber()-1) * pageSize;
        }
        positionParamsDto.setPageSize(pageSize);
        positionParamsDto.setPageNumber(pageNumber);
        List<PositionResponseDto> positionList = baseDao.getPositionList(positionParamsDto);
        for (PositionResponseDto paramsDto : positionList) {
            if (StringUtils.isNotBlank(paramsDto.getAttUrl())) {
                paramsDto.setAttUrl(CommonConstants.IMG_URL.concat(paramsDto.getAttUrl()));
            }
            paramsDto.setEducationName(EducationEnum.getNameByCode(paramsDto.getEducation()));
            paramsDto.setIsSuplifeName(IsSuplifeEnum.getNameByCode(paramsDto.getIsSuplife()));
            paramsDto.setWorkTimeName(ExperienceEnum.getNameByCode(paramsDto.getWorkTime()));
            paramsDto.setStdSalLoopName(StdSalLoopEnum.getNameByCode(paramsDto.getStdSalLoop()));
            paramsDto.setSalary(paramsDto.getSalaryMin().toString().concat("-").concat(paramsDto.getSalaryMax().toString()).concat(paramsDto.getStdSalLoopName()));
            paramsDto.setIsTravelExpensesName(IsTravelEnum.getNameByCode(paramsDto.getIsTravelExpenses()));
        }
        PageEntity page = getPage(positionParamsDto.getPageNumber(),positionParamsDto.getPageSize(),count,positionList);
        return page;
    }

    @Override
    public PositionResponseDto getPosition(Integer pracId) {
        PositionResponseDto positionResponseDto = baseDao.getPosition(pracId);
        positionResponseDto.setImageUrl(CommonConstants.ZHAOLEMO_LOGO);
        positionResponseDto.setIsCollect(NumberConstants.NUM_0);
        if (null != positionResponseDto.getEducation()) {
            positionResponseDto.setEducationName(EducationEnum.getNameByCode(positionResponseDto.getEducation()));
        }
        positionResponseDto.setIsSuplifeName(IsSuplifeEnum.getNameByCode(positionResponseDto.getIsSuplife()));
        if (ExperienceEnum.getWorkYearCodeList().contains(positionResponseDto.getWorkTime())) {
            positionResponseDto.setWorkTimeName(ExperienceEnum.getNameByCode(positionResponseDto.getWorkTime()));
        } else {
            positionResponseDto.setWorkTimeName(ExperienceEnum.WORK_UNDEFINED.getName());
        }
        positionResponseDto.setStdSalLoopName(StdSalLoopEnum.getNameByCode(positionResponseDto.getStdSalLoop()));
        positionResponseDto.setSalary(positionResponseDto.getSalaryMin().toString().concat("-").concat(positionResponseDto.getSalaryMax().toString()).concat(positionResponseDto.getStdSalLoopName()));
        positionResponseDto.setIsTravelExpensesName(IsTravelEnum.getNameByCode(positionResponseDto.getIsTravelExpenses()));
        return positionResponseDto;
    }

    @Override
    public PositionResponseDto getPosition(Integer empId, Integer pracId) {
        PositionResponseDto positionResponseDto = getPosition(pracId);
        int countPost = empPostCollectDao.countEmpPostCollect(pracId, empId);
        if (countPost > 0) {
            positionResponseDto.setIsCollect(1);
        }else {
            positionResponseDto.setIsCollect(0);
        }
        int count = empPositionSignDao.countEmpPositionSign(pracId, empId);
        if (count > 0) {
            positionResponseDto.setIsPost(1);
        }else {
            positionResponseDto.setIsPost(0);
        }
        return positionResponseDto;
    }
}
